package com.vipshop.view.adapter;


import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.vipshop.util.MyLog;
import com.vipshop.util.Utils;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;


/**
 *
 * @author simon
 *
 * @param <T>
 */

public abstract class MyBaseAdapter<T> extends ArrayAdapter<T> {


	protected Map<Integer, View> mViewMap;
	private LayoutInflater mInflater;
	private int mLayoutId;
	private boolean isFreshView;

	/**
	 * @param context
	 * @param resource
	 * @param objects
	 * @param requestFresh whether fresh the cache view
	 */
	public MyBaseAdapter(Context context, int resource,List<T> objects,boolean requestFresh) {
		super(context, resource, objects);
//		mViewMap = Collections.synchronizedMap(new LinkedHashMap<Integer, View>());
		mViewMap = new TreeMap<Integer, View>();
	    mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    mLayoutId = resource;
	    isFreshView = requestFresh;
	}

	public MyBaseAdapter(Context context, int resource) {
		super(context, resource);
//		mViewMap = Collections.synchronizedMap(new LinkedHashMap<Integer, View>());
		mViewMap = new TreeMap<Integer, View>();
	    mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    mLayoutId = resource;
	}


	/**
	 *
	 * @param context
	 * @param resource
	 * @param objects
	 */
	public MyBaseAdapter(Context context, int resource,List<T> objects) {
		this(context, resource, objects,false);
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view=null;
		T item = getItem(position);
		if(item!=null){
			Integer location = (int) getMapId(position, item);
			view = this.getViewMap(location,item);
			if(view == null){
				view = mInflater.inflate(mLayoutId, null, false);
				bindItemView(view,position,item);
				view.setTag(item);
				mViewMap.put(location, view);
			}
		}
		return view;
	}

	/**
	 *07-17 13:48:42.783: E/AndroidRuntime(16931): java.lang.IndexOutOfBoundsException: Invalid index 10, size is 0

	 * @param view
	 * @param position
	 * @param item
	 */
	protected void bindItemView(View view,int position, T item) {	}

	/**
	 *
	 * @param location
	 * @param item
	 * @return
	 */
	private View getViewMap(Integer location,T item){
		View view = null;
		if(!mViewMap.isEmpty()){
			if(mViewMap.containsKey(location)){
				view = mViewMap.get(location);
				if(view!=null){
					if(isFreshView)
						onRefresh(view,item);
				}
			}
		}
		return view;
	}

	protected void onRefresh(View view, T item) {
		// TODO Auto-generated method stub
	}

	/**
	 * @param position
	 * @param item
	 * @return the cache view key,default is position
	 */
	protected Integer getMapId(int position, T item) {
		// TODO Auto-generated method stub
		return position;
	}

	public void clearAllView(){
		if(mViewMap!=null)
			mViewMap.clear();
	}


	static class ViewHolder{
		View view;
	}
}